﻿using System;
using GameStructure.Contracts;
using GameStructure.Core;
using GameStructure.Types;
using GameStructure.Utilities;
using Tao.OpenGl;

namespace GameStructure.GaneState
{
    public class SpecialEffectState : IGameObject
    {
        private Font _font;
        private Text _text;
        private Renderer _renderer = new Renderer();
        private double _totalTime = 0;

        public SpecialEffectState(TextureManager textureManager)
        {
            _font = new Font(textureManager.Get("font"), FontParser.Parse(@"Resources\font.fnt"));
            _text = new Text("hello", _font);
        }

        public void Update(double elapsedTime)
        {
            const double frequency = 7;
            var xAdvance = 0;

            //var wavyNumberX = Math.Sin(_totalTime*frequency)*15;
            //var wavyNumberY = Math.Cos(_totalTime*frequency)*15;
            //var wavyNumberR = (float)Math.Sin(_totalTime * frequency);
            //var wavyNumberG = (float)Math.Cos(_totalTime * frequency);
            //var wavyNumberB = (float)Math.Sin(_totalTime + 0.25 * frequency);
            //wavyNumberR = 0.5f + wavyNumberR * 0.5f; // scale to 0-1
            //wavyNumberG = 0.5f + wavyNumberG * 0.5f; // scale to 0-1
            //wavyNumberB = 0.5f + wavyNumberB * 0.5f; // scale to 0-1
            //_text.SetColor(new Color(wavyNumberR, wavyNumberG, wavyNumberB, 1));
            //_text.SetPosition(wavyNumberX, wavyNumberY);
            //_totalTime += elapsedTime;

            foreach (var cs in _text.CharacterSprites)
            {
                var position = cs.Sprite.GetPosition();
                position.Y = 0 + Math.Sin((_totalTime + xAdvance) * frequency) * 25;
                cs.Sprite.SetPosition(position);
                xAdvance++;
            }

            _totalTime += elapsedTime;
        }

        public void Render()
        {
            Gl.glClearColor(0.0f,0.0f,0.0f,1.0f);
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
            _renderer.DrawText(_text);
            _renderer.Render();
        }
    }
}